package org.zephy.api;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestMain {
    public static void main(String[] args) throws Exception {
       /* System.out.println("开始测试Sm4加密解密......");
        String json= "{\"name\":\"静瑶\",\"描述\":\"测试SM4加密解密\"}";
        System.out.println("加密前:"+json);
        String key = "86C63180C2806ED1F47B859DE501215B";
        String cipher = Sm4Util.encryptEcb(key,json);//sm4加密
        System.out.println("加密后:"+cipher);
        //校验加密前后是否为同一数据
        System.out.println("检验:"+Sm4Util.verifyEcb(key,cipher,json));
        json=Sm4Util.decryptEcb(key,cipher);
        System.out.println("解密后："+json);
        System.out.println("结束======");*/
        /*List<String> sourceList=new ArrayList<>();
        for(int i=0;i<10000;i++){
            sourceList.add("第"+i+"条数据");
        }
        System.out.println("数据条数:"+sourceList.size());
        long a1=System.currentTimeMillis();
        for(int i=0;i<sourceList.size();i++){
            doSome();
        }
        long a2=System.currentTimeMillis();
        System.out.println("普通for循环用时:"+(a2-a1));

        long b1=System.currentTimeMillis();
        for(String str:sourceList){
            doSome();
        }
        long b2=System.currentTimeMillis();
        System.out.println("增加for循环用时:"+(b2-b1));
        long c1=System.currentTimeMillis();
        sourceList.forEach(t->doSome());
        long c2=System.currentTimeMillis();
        System.out.println("foreach循环用时:"+(c2-c1));

        long d1=System.currentTimeMillis();
        sourceList.parallelStream().forEach(t->doSome());
        long d2=System.currentTimeMillis();
        System.out.println("foreach-stream循环用时:"+(d2-d1));*/

        //stream集合遍历forEach
        List<String> list=new ArrayList<>();
        list.add("judy");
        list.add("Tom");

        list.stream().forEach(s->System.out.println(s));

        list.stream().forEach(s->{
            System.out.println(s);
            System.out.println(2);
        });
        //集合复杂对象遍历
        Student student=new Student();
        student.setAddress("泉州市丰泾区");
        student.setName("judy");
        student.setAge(11);
        Student s2=new Student();
        s2.setAddress("泉州市丰泾区");
        s2.setName("Michael");
        s2.setAge(22);
        List<Student> students=new ArrayList<>();
        students.add(student);
        students.add(s2);

        students.stream().forEach(student1->System.out.println(student1));

        //stream条件过滤filter
        List<String> a=new ArrayList<>();
        a.add("judy");
        a.add("Tom");
        a.add("  ");

        a.stream().filter(s->s.equals("judy")).forEach(s->System.out.println(s));
        a.stream().filter(s->!s.isEmpty()).forEach(s->System.out.println(s));
        //复杂对象多条件判断过滤
        Student stu=new Student();
        stu.setAddress("福建省泉州市");
        stu.setName("Michael");
        stu.setAge(33);
        List<Student> stuList=new ArrayList<>();
        stuList.add(stu);

        stuList.stream().filter(stu1->stu1.getAge()>28).forEach(stu1->System.out.println(stu1));
        stuList.stream().filter(s->s.getName().startsWith("Mich")).forEach(s->System.out.println(s));


        //stream元素映射map
        //将stream
        //流中的元素映射到另一个流中，这个是在后期经常用到的，比如方法所接收的返回值是A,但是接收的却是B
        //1.将String类型的流转换为Integer 类型
        Stream<String> stringStream=Stream.of("1","2","3","4","5","6");
        stringStream.map(str->Integer.parseInt(str)).forEach(System.out::println);
        //集合类型转换 在做权限处理要求权限不能重复，所以list集合转换为set集合
        List<String> aa=new ArrayList<>();
        aa.add("judy");
        aa.add("Tom");
        aa.add(" ");
        aa.stream().map(s->s).collect(Collectors.toSet()).forEach(s->System.out.printf(s));


        Stream<String> streamA=Stream.of("张无忌","张天师");
        Stream<String> streamB=Stream.of("不认识","汪认识");

        Stream.concat(streamA,streamB).forEach(System.out::println);
        Stream<String> result=Stream.concat(streamA,streamB);
        result.forEach(name->System.out.println(name));
    }
    private static void doSome(){
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
